package com.gitee.kenewstar.migration.core;

import cn.hutool.core.collection.CollUtil;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kenewstar
 */
@SuppressWarnings("all")
public interface RequestParamHandler {

    ThreadLocal<Map<String, List<String>>> params = new ThreadLocal<>();

    public static void addParamMap(Map<String, List<String>> paramMap) {
        params.set(paramMap);
    }

    public static void remove() {
        params.remove();
    }

    default String getParameter(String parameterName) {
        List<String> paramValues = params.get().get(parameterName);
        return CollUtil.isEmpty(paramValues) ? null : paramValues.get(0);
    }

    default Integer getParamInt(String parameterName) {
        return Integer.parseInt(getParameter(parameterName));
    }

    default Long getParamLong(String parameterName) {
        return Long.parseLong(getParameter(parameterName));
    }

    default Boolean getParamBool(String parameterName) {
        return Boolean.parseBoolean(getParameter(parameterName));
    }

    default List<String> getParameterList(String parameterName) {
        return params.get().get(parameterName);
    }

    default List<Long> getParamLongList(String parameterName) {
        return getParameterList(parameterName).stream().map(Long::parseLong).collect(Collectors.toList());
    }

}
